//
//  TitleViewController.m
//  礼物go
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TitleViewController.h"
#import "LikePostsViewController.h"
#import "LikePresentViewController.h"

static CGFloat const titleH = 44;
//static CGFloat const maxTitleScale = 1.3;


@interface TitleViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, weak) UIButton *selTitleButton;

@property (nonatomic, strong) NSMutableArray *buttons;

@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)NSMutableArray *lineArray;
@property(nonatomic, weak)UIView *selectLineView;

@end

@implementation TitleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.buttons = [NSMutableArray array];
    self.lineArray = [NSMutableArray array];
    
    // 1.创建小scrollview
    [self setupTitleScrollView];
    // 2.创建大scrollview
    [self setupContentScrollView];
    // 3.给大scrollview设置内容vc
    [self addChildViewController];
    // 4.布局按钮标题
    [self setupTitle];

    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREENWIDTH, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    self.contentScrollView.bounces = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.titleScrollView.scrollsToTop = NO;
    self.contentScrollView.scrollsToTop = NO;
    
    // Do any additional setup after loading the view.
}

// 设置头部标题
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
   // CGFloat y = self.navigationController ? navBarH : 0;
    CGRect rect = CGRectMake(0, 0, SCREENWIDTH, titleH);
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
   // titleScrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
    
}

#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, SCREENWIDTH, SCREENHeight);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:contentScrollView];
    
    
    self.contentScrollView = contentScrollView;
    
    
}

#pragma mark - 添加子控制器
- (void)addChildViewController{
    LikePresentViewController *presentVC = [[LikePresentViewController alloc] init];
    presentVC.title = @"喜欢的礼物";
    presentVC.view.frame = CGRectZero;
    [self addChildViewController:presentVC];
    presentVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHeight - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:presentVC.view];
    
    
    LikePostsViewController *postVC = [[LikePostsViewController alloc] init];
    postVC.title = @"喜欢的攻略";
    [self addChildViewController:postVC];
    postVC.view.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHeight - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:postVC.view];
}

// 布局按钮坐标
- (void)setupTitle{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = SCREENWIDTH / 2;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        //创建下划线View
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(x, titleH - 1, w, 1)];
        lineView.backgroundColor = [UIColor whiteColor];
        lineView.tag = i;
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        [self.lineArray addObject:lineView];
        [self.titleScrollView addSubview:lineView];
        
        // 设置默认第一个被选中
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;

}

// 按钮点击
- (void)chick:(UIButton *)btn{
    // 设置标题颜色为选中颜色
    [self selTitleBtn:btn];
    NSUInteger i = btn.tag;
    CGFloat x = i * SCREENWIDTH;
    
//    [self setUpOneChildViewController:i];
    
    // 点击按钮切换对应的vc
    self.contentScrollView.contentOffset = CGPointMake(x, 0);

}

// 选中按钮
- (void)selTitleBtn:(UIButton *)btn{
    //NSUInteger i = btn.tag;
    //先还原上次选中按钮的颜色和大小
       self.selectLineView.backgroundColor = [UIColor whiteColor];
    //获取View
    UIView *lineView = [self.lineArray objectAtIndex:btn.tag];
    //设置选中状态效果
    lineView.backgroundColor = [UIColor redColor];
    
    self.selectLineView = lineView;
    
}

//滑动到某个位置就加载相应的位置的vc,不多余创建
- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * SCREENWIDTH;
    
    UIViewController *vc = self.childViewControllers[i];
    
    //如果当前vc上有子视图就不再添加
    if (vc.view.superview) {
        return;
    }
    
    // 只是将vc的view加在大scrollview上
    vc.view.frame = CGRectMake(x, 0, SCREENWIDTH, SCREENHeight - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}

#pragma mark - UIScrollViewDelegate
// 滑动scrollview 改变按钮和vc
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 滑动scrollview得到当前下标
    NSUInteger i = self.contentScrollView.contentOffset.x / SCREENWIDTH;
    // 更新选中按钮的颜色和大小
    [self selTitleBtn:self.buttons[i]];
    // 切换vc
    [self setUpOneChildViewController:i];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
